package view;

import helper.Utils;

import java.awt.Color;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import model.Command;
import model.MinCommandSet;
import controller.ConfigController;
import controller.MemoryController;

@SuppressWarnings("serial")
public class ConfigView extends JPanel {
	private ConfigController configController = ConfigController.getInstance();

	public ConfigView() {
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setAlignmentX(LEFT_ALIGNMENT);
		this.add(makeContent());
	}

	private JComponent makeContent() {
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.setAlignmentX(LEFT_ALIGNMENT);
		panel.add(makeRegisterInfo());
		panel.add(makeCommandSet());
		return panel;
	}

	private JComponent makeRegisterInfo() {
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.setAlignmentX(LEFT_ALIGNMENT);
		panel.setBorder(BorderFactory.createTitledBorder("Register Width"));
		panel.add(new JLabel("Memory Width: "
				+ MemoryController.getInstance().getWidth()));
		panel.add(new JLabel("Opcode Width: "
				+ MemoryController.getInstance().getOpcodeWidth()));
		return panel;
	}

	private JComponent makeCommandSet() {
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.setAlignmentX(LEFT_ALIGNMENT);
		panel.setBorder(BorderFactory.createTitledBorder("Command Set"));
		panel.add(makeTable());
		return panel;
	}

	private JComponent makeTable() {
		String[] vars = { "$A", "$B", "$C", "$D", "$E", "$F" };
		String[] headlines = { "Opcode", "Name" };
		MinCommandSet minSet = configController.getMinCommandSet();
		String[][] content = new String[minSet.getCommands().size()][];

		int i = 0;
		for (Integer opcode : minSet.getCommands().keySet()) {
			Command current = minSet.getCommands().get(opcode);
			StringBuffer command = new StringBuffer(current.getName());
			if (current.getParameterCount() > 0) {
				command.append(" ");
				for (int j = 0; j < current.getParameterCount(); j++) {
					command.append(vars[j]);
					if (j < current.getParameterCount() - 1) {
						command.append(",");
					}
				}
			}
			content[i++] = new String[] { Utils.int2binaryString(opcode),
					command.substring(0, command.length()) };
		}

		JTable table = new JTable(content, headlines);

		table.setEnabled(false);
		table.setDefaultRenderer(Object.class, new GreyTableCellRenderer());
		JScrollPane pane = new JScrollPane(table);
		pane.setAlignmentX(LEFT_ALIGNMENT);
		pane
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		return pane;
	}

	public class GreyTableCellRenderer extends DefaultTableCellRenderer {
		@Override
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			Component cell = super.getTableCellRendererComponent(table, value,
					isSelected, hasFocus, row, column);

			cell.setBackground(new Color(238, 238, 238));

			return cell;

		}
	}

}
